package com.winning.sjzc.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName CommonUtils
 * @Description TODO
 * @Author WJX
 * @Date 2019/8/17 16:21
 **/
public class CommonUtils {

    /**
     * 通过正则表达式获得字符串中的数字
     * @param str
     * @return
     */
    private static Integer getNumbers(String str){
        String regEx = "[a-zA-Z]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return Integer.valueOf(m.replaceAll("").trim());
    }

    /**
     *
     * @param str
     * @return
     */
    public static Integer getDataSize(String str){
        Integer dataSize = 0;
        str = str.trim();
        if(StringUtils.isBlank(str) || StringUtils.equalsIgnoreCase("0",str)){
            return 0;
        }

        if(StringUtils.containsIgnoreCase(str,"k")){
            dataSize = getNumbers(str);
        }else if(StringUtils.containsIgnoreCase(str,"m")){
            dataSize = getNumbers(str)*1024;
        }else if(StringUtils.containsIgnoreCase(str,"g")){
            dataSize = getNumbers(str)*1024*1024;
        }else{
            dataSize = Integer.valueOf(str);
        }
        return dataSize;
    }
}
